<?php

namespace App\Http\Controllers;


use App\Exports\MultipleSheetsExport;
use App\Exports\MyExport;
use App\Exports\MyExportSheet1;
use App\Exports\MyExportSheet2;
use App\Imports\MyImport;
use Maatwebsite\Excel\Facades\Excel;

class ExcelController extends Controller
{

    protected $countData = [
        [
            '知道' => 0,
            '不知道' => 0,
        ],
        [
            '(跳过)' => 0,
            '其他' => 0,
            '公众号、新闻报道等' => 0,
            '政府部门网上发布的消息' => 0,
            '学院的宣传资料' => 0,
            '学院的教职工' => 0,
        ],
        [
            '(跳过)' => 0,
            '知道' => 0,
            '不知道' => 0,
        ],
        [
            '(跳过)' => 0,
            '培养目标与培养方案' => 0,
            '项目管理' => 0,
            '培养条件' => 0,
            '师资队伍' => 0,
            '教学组织' => 0,
            '培养质量' => 0,
            '社会效益' => 0,
            '办学特色' => 0,
        ],
        [
            '(跳过)' => 0,
            '培养目标' => 0,
            '培养方案' => 0,
            '管理机构' => 0,
            '资金管理' => 0,
            '招生和学籍管理' => 0,
            '教学质量监督' => 0,
            '文凭证书管理' => 0,
            '政策环境' => 0,
            '教学设施' => 0,
            '师资评聘' => 0,
            '师资状况' => 0,
            '师资培训' => 0,
            '教学计划' => 0,
            '教学大纲及材料' => 0,
            '教学方式' => 0,
            '教学文件及教学档案' => 0,
            '毕业成果质量鉴定' => 0,
            '学生满意度' => 0,
            '办学单位内部效益' => 0,
            '办学单位外部效益' => 0,
            '办学特色' => 0,
        ],
//        '9' => [
//            ''
//        ],
        [
            '没有必要' => 0,
            '有必要' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
        [
            '不知道' => 0,
            '合格' => 0,
            '不合格' => 0,
        ],
    ];


    public function index()
    {

        $path = base_path('storage/1.xlsx');

        $import = new MyImport();
        Excel::import($import, $path);
        $excelData = $import->excelData;
        $table = array_slice($import->table, 1);
        $arr1 = []; // 工业设计
        $arr2 = []; // 机械设计制造及其自动化
        $arr3 = []; // 生物工程
        foreach ($excelData as $item) {
            switch ($item[0]) {
                case "工业设计":
                    $arr1[] = $item;
                    break;
                case "机械设计制造及其自动化":
                    $arr2[] = $item;
                    break;
                case "生物工程":
                    $arr3[] = $item;
                    break;
            }
        }

        $arr4 = array_merge($arr2 , $arr3);

        // 总数量
        $total1 = count($arr1);
        $total2 = count($arr2);
        $total3 = count($arr3);
        $total4 = count($arr4);

        // 统计数量
        $data1 = $this->countHandle($arr1);
        $data2 = $this->countHandle($arr2);
        $data3 = $this->countHandle($arr3);
        $data4 = $this->countHandle($arr4);

        // 计算概率
        $proportion1 = $this->proportionHandle($data1, $total1);
        $proportion2 = $this->proportionHandle($data2, $total2);
        $proportion3 = $this->proportionHandle($data3, $total3);
        $proportion4 = $this->proportionHandle($data4, $total4);


        $result1 = $this->exportExcel($table, $data1, $proportion1);
        $result2 = $this->exportExcel($table, $data2, $proportion2);
        $result3 = $this->exportExcel($table, $data3, $proportion3);
        $result4 = $this->exportExcel($table, $data4, $proportion4);

//        return Excel::download(new MyExport($result1, $result2, $result3, $result4), "data.xlsx");
        return Excel::download(new MultipleSheetsExport($result1, $result2, $result3, $result4), "data.xlsx");
    }


    public function countHandle($data)
    {
        // 工业设计

        $newData = array_map(function ($item) {
            return array_slice($item, 1);
        }, $data);

        // 统计数量
        $arr = $this->countData;
        foreach ($newData as $data) {
            foreach ($data as $index => $item) {
                foreach ($arr[$index] as $key => $value) {
                    if ($index == 5 && strpos($item, $key) !== false) {
                        $arr[$index][$key] +=1;
                        continue;
                    }
                    if (in_array($index, [1, 3, 4]) && $item != "(跳过)") {
                        $tempArr = explode("┋",$item);
                        if (in_array($key , $tempArr)) {
                            $arr[$index][$key] +=1;
                            continue;
                        }
                    }
                    if ($item == $key) {
                        $arr[$index][$key] +=1;
                        continue;
                    }
                }
            }
        }
        return $arr;
    }


    public function proportionHandle($data, $total)
    {
        $result = [];
        foreach ($data as $index => $item) {
            $tempData = [];
            foreach ($item as $key => $value) {
                $proportion = round($value / $total, 4) * 100;
                $tempData[$key] = $proportion . "%";
            }
            $result[$index] = $tempData;
        }
        return $result;
    }


    public function exportExcel($table, $data, $proportion)
    {

        $result[] = [
            'id' => "题目+选项",
            'field1' => "数量",
            'field2' => " 比例",
        ];

        foreach ($data as $index => $item) {
            foreach ($item as $key => $value) {
                $dataKey = $table[$index] . '----' . $key;
                $result[] = [
                    'id' => $dataKey,
                    'filed1' => $value,
                    'field2' => $proportion[$index][$key],
                ];
            }
        }

        return $result;
    }
}
